﻿Imports CRM.BusinessLayer
Imports System.Data

Partial Public Class frmBrowseTasks
    Public Event ShowDisplayForm(ByVal userControlName As String)
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmBrowseTasks_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnNext.Click
        Me.txtTaskDate.Text = Format(CDate(Me.txtTaskDate.Text).AddDays(1), "dd-MMM-yyyy")
        Dim dt As DataTable = CommonDataFunctions.DaysTasks(Me.txtTaskDate.Text)
        DataBinding.BindListView(dt, Me.lvwTasks)
    End Sub

    Private Sub btnPrev_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPrev.Click
        Me.txtTaskDate.Text = Format(CDate(Me.txtTaskDate.Text).AddDays(-1), "dd-MMM-yyyy")
        Dim dt As DataTable = CommonDataFunctions.DaysTasks(Me.txtTaskDate.Text)
        DataBinding.BindListView(dt, Me.lvwTasks)
    End Sub

    Private Sub btnCalendar_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCalendar.Click

        Me.txtTaskDate.Text = CommonFuncs.GetPopUpCalendarDate(Me.txtTaskDate.Text)
        Dim dt As DataTable = CommonDataFunctions.DaysTasks(Me.txtTaskDate.Text)
        DataBinding.BindListView(dt, Me.lvwTasks)
    End Sub

    Private Sub frmBrowseTasks_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        If Not IsDate(Me.txtTaskDate.Text) Then
            Me.txtTaskDate.Text = Format(Now, "dd-MMM-yyyy")
        End If
        Me.Title = "Tasks for " & CommonDataFunctions.GetConsultantName(GlobalCRM.CurrentConViewID)
        'Me.Content = "Tasks for " & CommonDataFunctions.GetConsultantName(GlobalCRM.CurrentConViewID)
        Dim dt As DataTable = CommonDataFunctions.DaysTasks(Me.txtTaskDate.Text)
        If Not dt Is Nothing Then
            ' If dt.Rows.Count > 0 Then
            DataBinding.BindListView(dt, Me.lvwTasks)
            'End If
        End If


    End Sub

    Private Sub lvwTasks_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles lvwTasks.MouseDoubleClick

        If Me.lvwTasks.SelectedItems.Count = 0 Then
            Exit Sub
        End If

        Dim intEventLinkType As Integer
        intEventLinkType = Me.lvwTasks.SelectedItem.row.itemarray(1).ToString

        ' do different things depending on what task is linked to
        If intEventLinkType = TasksEvents.LinkTypes.Consultant Then
            CommonFuncs.DisplayMessage("This is a user task. It is not associated with a company, person or case.", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        ElseIf intEventLinkType = TasksEvents.LinkTypes.Company Then
            GlobalCRM.CurrentCompanyID = Me.lvwTasks.SelectedItem.row.itemarray(0).ToString
            GlobalCRM.CurrentSearchType = GlobalCRM.enuSearchType.eCompany
            GlobalCRM.TabSelection = GlobalCRM.enumSelectTab.eCompany
        ElseIf intEventLinkType = TasksEvents.LinkTypes.Person Then
            GlobalCRM.CurrentCandidateID = Me.lvwTasks.SelectedItem.row.itemarray(0).ToString
            GlobalCRM.CurrentSearchType = GlobalCRM.enuSearchType.ePerson
            GlobalCRM.TabSelection = GlobalCRM.enumSelectTab.ePerson
        ElseIf intEventLinkType = TasksEvents.LinkTypes.eCase Then
            GlobalCRM.CurrentCaseID = Me.lvwTasks.SelectedItem.row.itemarray(0).ToString
            GlobalCRM.CurrentSearchType = GlobalCRM.enuSearchType.eCase
            GlobalCRM.TabSelection = GlobalCRM.enumSelectTab.eCase
        End If

        'frmMainDisplay.MdiParent = frmMDIMain
        'frmMainDisplay.Show()
        'RaiseEvent ShowDynamicTab("maindisplay")
        Me.Close()

    End Sub

    Private Sub txtTaskDate_SelectedDateChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles txtTaskDate.SelectedDateChanged
        Me.txtTaskDate.Text = (New CommonFuncs).FormatDate(txtTaskDate.SelectedDate)
        Dim dt As DataTable = CommonDataFunctions.DaysTasks(Me.txtTaskDate.Text)
        DataBinding.BindListView(dt, Me.lvwTasks)
    End Sub
End Class
